package com.zxp.common.core.validatecode.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * 验证码基础信息
 *
 * @author chentongwei@bshf360.com 2018-05-25 11:40
 */
@Data
@NoArgsConstructor @AllArgsConstructor
public class ValidateCode implements Serializable {

    /** 验证码 */
    private String code;
    /** 过期时长 */
    private LocalDateTime expireTime;

    public ValidateCode(String code, int expireIn) {
        this.code = code;
        this.expireTime = LocalDateTime.now().plusSeconds(expireIn);
    }

    public boolean isExpired() {
        return LocalDateTime.now().isAfter(expireTime);
    }

    // 获取还要多久过期
    public Long getExpireSends(){
        // 获取秒数时间戳（10位）
        long now = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long l = expireTime.toInstant(ZoneOffset.of("+8")).toEpochMilli() - now;
        if(l>0){
            return l;
        }
        return 7200L;
    }

}
